package com.hitqz.robot.biz.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolItemVo;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolPosPoseGroupVo;
import com.hitqz.robot.api.common.entity.PatrolPosPoseGroup;
import com.hitqz.robot.api.common.entity.PatrolPose;
import com.hitqz.robot.biz.service.PatrolItemService;
import com.hitqz.robot.biz.service.PatrolPosPoseGroupService;
import com.hitqz.robot.biz.service.PatrolPoseService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */

@RestController
@AllArgsConstructor
@RequestMapping("/patrolPosPoseGroup")
@Tag(name = "巡检位置-姿态组关系管理")
public class PatrolPosPoseGroupController {

    private final PatrolPosPoseGroupService patrolPosPoseGroupService;

    private final PatrolPoseService patrolPoseService;

    private final PatrolItemService patrolItemService;
    

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询巡检位置-姿态组关系")
    public R<PatrolPosPoseGroupVo> getById(@PathVariable("id") Integer id) {
        PatrolPosPoseGroupVo vo = patrolPosPoseGroupService.getVoById(id);
        Integer patrolPoseGroupId = vo.getPatrolPoseGroupId();
        if (patrolPoseGroupId!= null) {
            List<PatrolPose> poseList = patrolPoseService.list(Wrappers.<PatrolPose>lambdaQuery().eq(PatrolPose::getGroupId, patrolPoseGroupId));
            vo.setMarkStatusStr(poseList.isEmpty()? "未标定" : "已标定");
        }
        return R.ok(vo);
    }

    @GetMapping("/{id}/patrolItem")
    @Operation(summary = "根据id查询巡检位置-姿态组关系下的检测项")
    public R<List<PatrolItemVo>> getItemVoListById(@PathVariable("id") Integer id) {
        List<PatrolItemVo> voList = patrolItemService.getVoListByPoseGroupId(id);
        return R.ok(voList);
    }

    @PostMapping
    @Operation(summary = "新增巡检位置-姿态组关系")
    public R save(@RequestBody @Validated PatrolPosPoseGroup dto) {
        return R.ok(patrolPosPoseGroupService.save(dto));
    }

    @PutMapping
    @Operation(summary = "修改巡检位置-姿态组关系")
    public R updateById(@RequestBody @Validated PatrolPosPoseGroup dto) {
        return R.ok(patrolPosPoseGroupService.updateById(dto));
    }

    @DeleteMapping
    @Operation(summary = "根据id删除巡检位置-姿态组关系")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(patrolPosPoseGroupService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
